var path = require('path');
var fs = require('fs');
var promise = require('promise');
var request = require('request');
request.debug = true;
require('request-debug')(request);

var __srcFolder = path.join(__dirname, 'data/');

function DzdpDataParser() {
}
/**inner function*/
function __promisePostJson(url, strBody) {
    return new Promise(function (resolve, reject) {
        request({
            method: 'POST',
            url: url,
            body: JSON.parse(strBody),
            json: true,
            headers: {
                "content-type": "application/json"
            }
        }, function (err, response, body) {
            //if (err) return reject(err);
            if (err) {
                console.log('post json err' + err);
                throw err;
            }
            return resolve(body);
        });
    });
}


function __promiseReadFileUtf8(path) {
    return new Promise(function (resolve, reject) {
        fs.readFile(path, 'utf8', function (err, res) {
            //if (err) return reject(err);
            if (err) throw err;
            resolve(res);
        })
    });
}


DzdpDataParser.prototype.find_businesses = function () {
    console.log('find_businesses');
    var name = 'find_businesses.json';
    var url = 'http://localhost:8001/api/v1/shop';
    var filePath = path.join(__srcFolder, name);
    console.log(filePath);
    __promiseReadFileUtf8(filePath).then(function fullfil(res) {
        //console.log(res);
        console.log('read file succ');
        var obj = JSON.parse(res);
        var businesses = obj.businesses;
        console.log(typeof businesses);
        if (typeof businesses === 'string') {
            businesses = JSON.parse(businesses);
        }
        var shops = [];
        businesses.forEach(function (item, index) {
            if (index === 0) {
                console.log('----item[0] check-----');
                //console.log(item);
                console.log(item.name);
            }

            var aShop = {
                "name": item.name,
                "address": item.address,
                "telephone": item.telephone,
                "images_url": [item.photo_url, item.s_photo_url],
                "province_code": null,
                "province_name": null,
                "city_code": null,
                "city_name": item.city,
                "distinct_code": null,
                "distinct_name": null,
                "region": item.regions,
                "loc": [item.latitude, item.longitude],
                // },

                "ware_categories": null,
                "ware_ids": null,

                // 评分
                "decoration_grade": 0,
                "product_grade": 0,
                "service_grade": 0,
                "review_count": 0,
                extra_info: JSON.stringify(item)
            };
            shops.push(aShop);
        });
        //console.log(shops[0]);

        shops.forEach(function (item, index) {
            //if (index > 0) {
            //    return;
            //}
            var strBody = JSON.stringify(item);
            console.log(strBody);
            __promisePostJson(url, strBody)
                .then(function (res) {
                    console.log('---------res---------');
                    console.log(res);
                });
        });
    }).catch(function (ex) {
        console.log(ex);
    });
};


var dzdpParser = new DzdpDataParser();
dzdpParser.find_businesses();